PCA and calcium indicators

SETUP WORKSPACE - ADD FOLDERS TO PATH

close all; clear all; clear java; clc; rng('shuffle');
h='/Users/bradleymonk/Documents/MATLAB/GIT/grin/grinadina';
p=what(h); mypath{1}=p.path;
mypath{2} = [':' p.path filesep 'imagingdata'];
addpath(cell2mat(mypath)); cd(p.path);

GET PATHS OF TIF FILES IN SOME FOLDER (SPECIFIED ABOVE)

p=what('imagingdata');
fileinfo = dir(p.path);
allfilenames = {fileinfo.name};
c=~cellfun(@isempty,regexp(allfilenames,'((\S)+(\.tif+))'));
finames = string(allfilenames(c)');
fipaths = fullfile(p.path,finames);
clc; fprintf('\n %s/...\n\n', p.path);
/Users/bradleymonk/Documents/MATLAB/GIT/grin/grinadina/imagingdata/...
fprintf('\t\t %s \n', finames);
C2-TSeries-05292018-1348-130-no-stim.tif
TSeries-05292018-1348-130-no-stim.tif
TSeries-05292018-1348-130.tif
TSeries-05292018-1348-131.tif
TSeries-05292018-1348-132.tif
TSeries-05292018-1348-133.tif
fauxGCaMP.tif

SELECT A TIFF STACK AND GET INFO

IM_path = fipaths{1};
IM_info = struct2table(imfinfo(IM_path));
IM_count = height(IM_info);
IM_series = IM_info.Filename;
clc;
s0 = fprintf('\nSelected Tiff file: \n %s \n', IM_path );
Selected Tiff file:
/Users/bradleymonk/Documents/MATLAB/GIT/grin/grinadina/imagingdata/C2-TSeries-05292018-1348-130-no-stim.tif
s1 = sprintf('\nTiff file has the following properties ' );
s2 = sprintf('\nColormap: %.f x %.f ', size(IM_info.Colormap{1}));
s3 = sprintf('\nColors: %s ', IM_info.ColorType{1});
s4 = sprintf('\nWidth: %.f ', IM_info.Width(1));
s5 = sprintf('\nHeight: %.f ', IM_info.Height(1));
s6 = sprintf('\nBytes: %.f ', IM_info.FileSize(1));
s7 = sprintf('\nBits: %.f ', IM_info.BitDepth(1));
s8 = sprintf('\nXRes: %.f ', IM_info.XResolution(1));
s9 = sprintf('\nYRes: %.f \n', IM_info.YResolution(1));
disp([s0 s1 s2 s3 s4 s5 s6 s7 s8 s9])
Tiff file has the following properties
Colormap: 256 x 3
Colors: grayscale
Width: 256
Height: 256
Bytes: 38194256
Bits: 16
XRes: 362
YRes: 362

IMPORT ONE OF THOSE TIFF STACKS

warning('off')
TifLink = Tiff(IM_info.Filename{1});
for i=1:height(IM_info)
TifLink.setDirectory(i);
IMG(:,:,i)=TifLink.read();
end
TifLink.close();
disp('Stack size...'); disp(size(IMG)); syze(IMG)
Stack size...
256 256 291


Shape: 256 256 291
Class: uint16

--BYTES--
37248 Kb
36.38 Mb
0.03552 Gb

PREVIEW IMAGE STACK

clearvars -except IMG IM_info ICEL
% MAKE ANIMATED GIF
fh=figure('Units','normalized','OuterPosition',[.1 .1 .4 .6],'Color','w');
ah=axes('Position',[.05 .05 .9 .9],'Color','none');
ph1 = imagesc(IMG(:,:,1));

RUN PCA

% FIRST RESHAPE IMAGE STACK INTO A SINGLE MATRIX
IM = reshape(IMG,1,[],291);
IM = squeeze(IM);
IM = single(IM);
[COEF,SCORE,~] = pca(IM');
[COEF2,SCORE2,~] = pca(IM);
clc;clearvars -except fipaths IPATH IMG COEF SCORE...
COEF2 SCORE2 ICOEF ISCORE2

RESHAPE COEF BACK INTO A STACK

ICOEF = reshape(COEF,size(IMG,1),size(IMG,2),size(IMG,3)-1);
ICOEF = ICOEF(:,:,1:25); % GET THE FIRST 25 COMPONENTS
ISCORE2 = reshape(SCORE2,size(IMG,1),size(IMG,2),[]);
ISCORE2 = ISCORE2(:,1:25); % GET THE FIRST 25 COMPONENTS
clc;clearvars -except fipaths IPATH IMG COEF SCORE...
COEF2 SCORE2 ICOEF ISCORE2

PLOT PCA SCATTER / LINE GRAPHICS

clc; close all;
imagesc(mean(ICOEF,3))
imagesc(max(ICOEF,[],3))
% clc; close all;
% scatter(COEF(:,1),COEF(:,2))
fh1 = figure('Units','normalized','OuterPosition',[.01 .06 .7 .7],'Color','w');
ax1 = axes('Position',[.05 .09 .42 .83],'Color','none');
ax2 = axes('Position',[.55 .09 .42 .83],'Color','none');
axes(ax1)
ph1=scatter(COEF(:,1),COEF(:,2));
axes(ax2)
ph2=scatter3(COEF(:,1),COEF(:,2),COEF(:,3));
view([-56.3 34.8])
axis vis3d

DISPLAY JOINT IMAGE WITH FRAME+FRAME_ABOVE

clc; close all;
fh1=figure('Units','normalized','OuterPosition',[.1 .1 .7 .9],'Color','w','MenuBar','none');
hax1 = axes('Position',[.05 .05 .9 .9],'Color','none');
Ix = imfuse(ICOEF(:,:,1),ICOEF(:,:,2),...
'falsecolor','Scaling','joint','ColorChannels',[2 1 0]);
Ij = imfuse(ICOEF(:,:,2),ICOEF(:,:,3),...
'falsecolor','Scaling','joint','ColorChannels',[2 1 0]);
Ik = imfuse(ICOEF(:,:,3),ICOEF(:,:,4),...
'falsecolor','Scaling','joint','ColorChannels',[2 1 0]);
Il = imfuse(ICOEF(:,:,4),ICOEF(:,:,5),...
'falsecolor','Scaling','joint','ColorChannels',[2 1 0]);
subplot(2,2,1), imagesc(Ix); axis square;
subplot(2,2,2), imagesc(Ij); axis square;
subplot(2,2,3), imagesc(Ik); axis square;
subplot(2,2,4), imagesc(Il); axis square;